<template>
  <div style="margin: 1rem">
    <a href="/">返回首页</a>
    <br />
    <h1>用户首页</h1>
  </div>
  <!-- 已经登录的情况 -->
  <div v-if="islogin" class="div" v-loading="loading">
    <div>
      <el-dropdown @command="handlerCommand">
        <div>
          <el-avatar :src="user.logo" :size="100"></el-avatar>
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item
              ><el-icon><User /></el-icon
              >{{ user.tbUser.username }}</el-dropdown-item
            >

            <el-dropdown-item divided command="home"
              ><el-icon><HomeFilled /></el-icon>网站首页</el-dropdown-item
            >

            <el-dropdown-item divided command="userlogo"
              ><el-icon><User /></el-icon>修改头像</el-dropdown-item
            >

            <el-dropdown-item command="userpwd"
              ><el-icon><Unlock /></el-icon>修改密码</el-dropdown-item
            >

            <el-dropdown-item command="userinfo"
              ><el-icon><Setting /></el-icon>修改简介</el-dropdown-item
            >

            <el-dropdown-item divided command="logout"
              ><el-icon><CloseBold /></el-icon>退出登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <div>
      昵称：{{ user.tbUser.username }}
      <br />
      性别：{{ user.tbUserInfo.sex }}
    </div>
  </div>

  <!-- 修改密码的对话框 -->
  <div>
    <el-dialog v-model="dialogPwd" title="修改密码" width="300px">
      <div>
        <el-form>
          <el-form-item>
            <el-input
              v-model="passwordInfo.oldpwd"
              placeholder="请输入旧密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="passwordInfo.newpwd"
              placeholder="请输入新密码"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dotpwd">确定</el-button>
          <el-button type="danger" @click="dialogPwd = false"> 取消 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>

  <!-- 没有登录的情况 -->
  <div v-if="!islogin">
    <el-button-group>
      <el-button type="primary" @click="toPage('/user/Login')">登录</el-button>
      <el-button type="success" @click="toPage('/user/Reg')">注册</el-button>
    </el-button-group>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { tools } from '../../js/tools'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { api } from '../../js/ajax'

const router = useRouter()
const loading = ref(false)

const toPage = page => {
  router.push(page)
}

const handlerCommand = command => {
  if (command == 'logout') {
    // 安全退出
    api.delete('/auth', {}, () => {
      queryUser()
    })
  } else if (command == 'home') {
    router.push('/')
  } else if (command == 'userlogo') {
    modifyUserLogo()
  } else if (command == 'userpwd') {
    btndotpwd()
  }
}

const dialogPwd = ref(false)
const passwordInfo = ref({
  oldpwd: '',
  newpwd: '',
})
const btndotpwd = () => {
  passwordInfo.value = {
    oldpwd: '',
    newpwd: '',
  }
  dialogPwd.value = true
}
const dotpwd = () => {
  api.patch('/user/password', passwordInfo.value, data => {
    if (data.success) {
      ElMessage.success('修改密码成功')
      passwordInfo.value = {
        oldpwd: '',
        newpwd: '',
      }
      return
    }
    ElMessageBox.alert(data.message, '修改密码')
  })
}

const modifyUserLogo = () => {
  tools.openImage(file => {
    loading.value = true
    api.uploadUserLogo(file, data => {
      loading.value = false
      if (data.success) {
        queryUser()
        return
      }
      ElMessageBox.alert(data.message, '修改头像失败')
    })
  })
}

const islogin = ref(false)
const user = ref({
  tbUser: {},
  tbUserInfo: {},
  tbRole: {},
  exInfo: {},
  logo: '',
})

const queryUser = () => {
  api.get('/auth', {}, data => {
    islogin.value = data.success
    if (data.success) {
      user.value = data.data
      user.value.logo = api.userlogo(data.data.tbUser.uid)
    } else {
      user.value = {
        tbUser: {},
        tbUserInfo: {},
        tbRole: {},
        exInfo: {},
        logo: '',
      }
    }
  })
}
queryUser()
</script>

<style scoped>
.div {
  margin: 1em;
  display: flex;
}
.div div:first-of-type {
  margin-right: 1rem;
}
.el-dropdown img {
  width: 5rem;
  height: auto;
  border-radius: 2.5rem;
}
.el-tooltip__trigger {
  outline: none;
}
.el-dialog {
  width: 25%;
}
</style>
